//
//  LunBo.m
//  news
//
//  Created by imacbookair on 16/3/9.
//  Copyright © 2016年 imacbookair. All rights reserved.
//

#import "LunBo.h"

@implementation LunBo

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame URLArr: (NSMutableArray *)arr timeInterval: (NSTimeInterval)timeInterval pageFrame: (CGRect)pageFrame  arr: (NSMutableArray *)arrTemp;
{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView = [[UIScrollView alloc]init];
        self.page = [[UIPageControl alloc] init];
        
        
        // 传入Frame
        _scrollView.frame = frame;
        // 代理人
        _scrollView.delegate = self;
        // 可以整页滚动
        _scrollView.pagingEnabled = YES;
        
        // 加入第一张图片  最后一张图片
        
        id firstImage = [arr firstObject];
        id lastImage = [arr lastObject];
        
        [arr addObject:firstImage];
        [arr insertObject:lastImage atIndex:0];
        
        id firstLabel = [arrTemp firstObject];
        id lastLabel = [arrTemp lastObject];
        
        [arrTemp addObject:firstLabel];
        [arrTemp insertObject:lastLabel atIndex:0];
        
        
        NSLog(@"!!%ld, ~~~%ld", arr.count, arrTemp.count);
        
        // 滚动视图的大小
        _scrollView.contentSize = CGSizeMake(self.bounds.size.width * arr.count, 0);
        _scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        
        [self addSubview:self.scrollView];
        [self addSubview:self.page];
        // 轮播图
        for (NSInteger i = 0; i < arr.count; i++) {
            
            UIView *bjView = [[UIView alloc]initWithFrame:CGRectMake(0, 170, 375, 30)];
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 170, 375, 30)];
            label.text = arrTemp[i];
            label.textColor = [UIColor whiteColor];
            bjView.backgroundColor = [UIColor grayColor];
            
            //调试背景颜色亮度
            bjView.alpha = 0;
            
            _imgView = [[UIImageView alloc]initWithFrame:CGRectMake(self.bounds.size.width * i, 0, self.bounds.size.width, self.frame.size.height)];
            [_imgView sd_setImageWithURL:[NSURL URLWithString:arr[i]]];
            
            
            [_imgView addSubview:label];
            [_imgView addSubview:bjView];
            [_scrollView addSubview:_imgView];
        }
        
        // 圆点按钮  页面控制器
        self.page = [[UIPageControl alloc]initWithFrame:pageFrame];
        
        
        self.page.numberOfPages = arr.count - 2;
        
        [_page addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(autoTime:) userInfo:nil repeats:YES];
        self.index = 0;
        self.arr = arr;
        
    }
    return self;
}
#pragma mark - scrollView协议方法
/// 设置页面控制器所在位置
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.page.currentPage = (scrollView.contentOffset.x - self.scrollView.frame.size.width) / scrollView.frame.size.width;
}
/// 当手指离开的时候开始滑动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(autoTime:) userInfo:nil repeats:YES];
    
}
/// 当手指接触的时候让定时器停止
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer invalidate];
    self.timer = nil;
    
}

/// 定时器在不断循环滚动, 就不会减速停止, 此方法就不会走
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.scrollView == scrollView) {
        // 每次减速结束都让scrollView所有的scrollView的子视图缩放变为1
        for (UIScrollView *viewTemp in scrollView.subviews) {
            if ([viewTemp isKindOfClass:[UIScrollView class]]) {
                viewTemp.zoomScale = 1.0;
                
            }
        }
        if (self.scrollView.contentOffset.x >= self.scrollView.frame.size.width * ( self.arr.count - 1)) {
            NSLog(@"最后一页");
            [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0) animated:NO];
            
        }
        if (self.scrollView.contentOffset.x <= 0) {
            [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width * (self.arr.count - 2), 0) animated:NO];
            
        }
    }
    
    // 不管怎么移动, 保证self.index 存放的是当前的页码
    self.index = self.scrollView.contentOffset.x / self.scrollView.frame.size.width - 1;
}


#pragma mark - private方法
/// 定时器方法
- (void)autoTime: (NSTimer *)timer
{
    // 使用scrollView自带的滚动动画, 实现起来效果不好, 有bug. 所以需要自己写动画效果
    
    [UIView animateWithDuration:0.5 animations:^{
        [self.scrollView setContentOffset:CGPointMake((self.index + 1) * self.scrollView.frame.size.width, 0)];
    }];
    
    self.index++;
    // 如果滑到最后一页
    if (self.scrollView.contentOffset.x == (self.arr.count - 1) * self.scrollView.frame.size.width) {
        // 快速移动到第一页
        [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0)];
        self.index = 0;
        
    }
}
/// 改变页面控制器的位置
- (void)changePage: (UIPageControl *)page
{
    [self.scrollView setContentOffset:CGPointMake((page.currentPage + 1) * self.scrollView.frame.size.width, 0) animated:YES];
}

@end
